# Virtual Router Simulator 
#
# Copyright (C) 2009-2010 VRSim Project
# Author: Choonho Son <choonho@kt.com>
# URL: <http://vrsim.googlecode.com>
# For license information, see LICENSE.TXT
#
import VRSim.Globals as globals
from PyQt4 import QtGui,  QtCore
from VRSim.Ui.Ui_DynamipsConfig import Ui_DynamipsConfig

class DynamipsConfig(QtGui.QDialog,  Ui_DynamipsConfig):
    def __init__(self,  pnode):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.pnode = pnode
        
        #############
        # intilize contents
        #############
        self.initialize()
        
        self.connect(self.pushButton_cfg,  QtCore.SIGNAL('clicked()'),  self.__chooseCfg)
        self.connect(self.pushButton_chImg,  QtCore.SIGNAL('clicked()'),  self.__chooseImg )        
        self.connect(self.buttonBox,  QtCore.SIGNAL('accepted()'),  self.__save )
        self.connect(self.buttonBox,  QtCore.SIGNAL('rejected()'),  self.close )
    def initialize(self):
        # hypervisor type
        count = 0
        for item in self.pnode.type_list:
            self.comboBox_Type.addItem(item)
            if self.pnode.type == item:
                self.comboBox_Type.setCurrentIndex(count)
            count += 1
        
         # hypervisor selection
        hypervisors = globals.GApp.topology.hypervisors
        # [name, type, uri]
        # selected item
        vmName = globals.GApp.topology.nodes[self.pnode.nodeid].vm
        index = 0
        for item in hypervisors:
            type = item[1]
            if type == "Dynamips":
                self.comboBox_Hypervisor.addItem(item[0])
                if vmName == item[0]:
                    self.comboBox_Hypervisor.setCurrentIndex(index)
                index = index + 1
        
        # hostname
        self.hostname = self.pnode.hostname
        #self.lineEdit_Hostname.setText(self.pnode.hostname)
        
        # Console
        self.label_Console.setText(str(self.pnode.console))
        # idlepc
        
        # NPE
        count = 0
        for item in self.pnode.npetype_list:
            self.comboBox_Npetype.addItem(item)
            if self.pnode.npetype == item:
                self.comboBox_Npetype.setCurrentIndex(count)
            count += 1
        
        # Midplane
        count = 0
        for item in self.pnode.midplane_list:
            self.comboBox_Midplane.addItem(item)
            if self.pnode.midplane == item:
                self.comboBox_Midplane.setCurrentIndex(count)
            count += 1
            
        # confg
        if self.pnode.cfg:
            self.label_cfg.setText(self.pnode.cfg)
        if self.pnode.hdd:
            self.label_selectedImage.setText(self.pnode.hdd)
        
        # slot display
        display_list = [self.label_slot0IF,  self.label_slot1IF,  self.label_slot2IF,  self.label_slot3IF,  self.label_slot4IF, 
                        self.label_slot5IF,  self.label_slot6IF]
        for slot in range(len(self.pnode.slots)):
            ports = ''
            for item in self.pnode.slots[slot]:
                ports += item
            display_list[slot].setText(ports)
        
        
    ##################################
    # Signal
    ##################################
    def __chooseImg(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, "Open file",  "/data/testbed")
        self.label_selectedImage.setText(filename)
      
    def __chooseCfg(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, "Open Config file",  "/data/testbed")
        self.label_cfg.setText(filename)
        
    def __save(self):
        # hypervisor name
        globals.GApp.topology.nodes[self.pnode.nodeid].vm = str( self.comboBox_Hypervisor.currentText() )
        
        print "Save"
        pnode = globals.GApp.topology.nodes[self.pnode.nodeid].physnode
        pnode.type = self.comboBox_Type.currentText()
        pnode.name = self.hostname
        
        # HDD
        pnode.cfg = self.label_cfg.text()
        pnode.hdd = self.label_selectedImage.text()
        
        # NIC
        
        self.close()
        

        
        
       
        
        
        

      
